package com.example.yskjproject.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.viewbinding.ViewBinding;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.yskjproject.R;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.QRBindWxModel;
import com.example.yskjproject.model.WxLoginModel;
import com.example.yskjproject.model.WxScanCodeModel;
import com.example.yskjproject.ui.fragment.UserInfoFragment;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.utils.QRCodeUtil;
import com.example.yskjproject.utils.SpCashUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * WXCodeDialog
 * <p>
 * 作者：zx
 * 创建时间：2024/5/18
 */
public class WXCodeDialog extends Dialog{

    private WxScanCodeModel wxScanCodeModel;
    private Gson gson = new Gson();
    private Context context;

    public WXCodeDialog(@NonNull Context context, WxScanCodeModel wxScanCodeModel) {
        super(context);
        setContentView(R.layout.wx_code);
        this.wxScanCodeModel = wxScanCodeModel;
        this.context = context;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8); // 设置宽度为500dp
            layoutParams.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels * 0.28); // 设置高度为300dp
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setAttributes(layoutParams);
        }
        AppCompatImageView imageView = findViewById(R.id.img_code);
        Glide.with(context).load(wxScanCodeModel.getImg_url())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))//圆角半径
                .into(imageView);
        SpCashUtils.getInstance().setTicket(wxScanCodeModel.getTicket());
        setHandler();
    }

    private void setHandler() {
        if (mHandler != null) {
            mHandler.removeMessages(1);
            mHandler.sendEmptyMessage(1);
        }
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Map<String, String> jsonParams = new HashMap<>();
                jsonParams.put("ticket", SpCashUtils.getInstance().getTicket());
                jsonParams.put("device_token", SpCashUtils.getInstance().getDeviceToken());
                String paramsDate = gson.toJson(jsonParams);
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
                OkGo.<LzyResponse<QRBindWxModel>>post(Urls.LOOP_WX_USER).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<QRBindWxModel>>() {
                    @SuppressLint("SimpleDateFormat")
                    @Override
                    public void onSuccess(Response<LzyResponse<QRBindWxModel>> response) {
                        QRBindWxModel wxLoginModel = response.body().data;
                        if (wxLoginModel != null) {
                            switch (wxLoginModel.getBind_state()) {
                                case 0://未扫码
                                    mHandler.removeMessages(1);
                                    //间隔时间
                                    mHandler.sendEmptyMessageDelayed(1, 1000);
                                    break;
                                case 1://成功  直接登录
                                    EventBusUtil.sendEvent(new EventMessage(EventContent.upDate_user));
                                    dismiss();
                                    break;

                            }
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<QRBindWxModel>> response) {
                        super.onError(response);
                    }
                });

            }
        }
    };
}
